import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
    {path:'/',redirect:"/login"},
    {path:'/login',component:Login},
    {path:'/Home',component:Home}
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
    if(to.path == '/login'){ // 如果路径是login页面,所有人都可登录
        return next()
    }
    if(sessionStorage.token){ // 如果不是登录页面，判断sessionStorage是否有token
        return next()
    }
    return next({path:'/login'}) // 如果不是login页且没有token，转到login页面。
})

export default router
